package com.joy.web.admin.controller;

import com.joy.common.exception.JsonException;
import com.joy.common.model.ApiResponse;
import com.joy.web.admin.service.IResourceService;
import com.joy.web.admin.service.IUserService;
import com.joy.common.constant.CommonCons;
import com.joy.common.enums.Status;
import com.joy.web.base.controller.BaseController;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @ClassName: CacheController
 * @Description: 缓存管理
 * @Author: yjzhao
 * @Create: 2019-09-18  14:07
 **/
@Controller
@RequestMapping("/admin/cache")
public class CacheController extends BaseController {

    @Resource
    IResourceService resourceService;

    @Resource
    IUserService userService;


    /**
     * 用户管理初始化页面
     * @return
     */
    @GetMapping(value = "")
    public String index(ModelMap map) throws Exception {
        return "pages/admin/cache/index";
    }

    /**
     * 清空所有缓存
     * @return
     */
    @Caching(evict = {
            @CacheEvict(value= CommonCons.MyCache.USERINFOS, allEntries = true),
            @CacheEvict(value= CommonCons.MyCache.MENUTREE, allEntries = true),
            @CacheEvict(value= CommonCons.MyCache.MENU_URLS, allEntries = true),
            @CacheEvict(value= CommonCons.MyCache.ACTION_LIKE_KEYS, allEntries = true)
    })
    @PostMapping(value = {"/cleanAll"})
    @ResponseBody
    public ApiResponse cleanAll() throws Exception {
        userService.removeUserAuthorization();
        return ApiResponse.ofSuccess("缓存清除完成");
    }

    /**
     * 清空用户缓存
     * @return
     */
    @CacheEvict(value= CommonCons.MyCache.USERINFOS, allEntries = true)
    @ResponseBody
    @PostMapping(value = {"/cleanUserInfos"})
    public ApiResponse cleanUserInfos() throws Exception {
        return ApiResponse.ofSuccess("缓存清除完成");
    }

    /**
     * 清空资源缓存
     * @return
     */
    @Caching(evict = {
            @CacheEvict(value= CommonCons.MyCache.MENUTREE, allEntries = true),
            @CacheEvict(value= CommonCons.MyCache.MENU_URLS, allEntries = true),
            @CacheEvict(value= CommonCons.MyCache.ACTION_LIKE_KEYS, allEntries = true)
    })
    @ResponseBody
    @PostMapping(value = {"/cleanResources"})
    public ApiResponse cleanResources() throws Exception {
        return ApiResponse.ofSuccess("缓存清除完成");
    }

    /**
     * 清空权限缓存
     * @return
     */

    @ResponseBody
    @PostMapping(value = {"/cleanPermissions"})
    public ApiResponse cleanPermissions() throws Exception {
        userService.removeUserAuthorization();
        return ApiResponse.ofSuccess("缓存清除完成");
    }

    /**
     * 新增初始化 权限缓存
     * @return
     */
    @PostMapping(value = {"/addResources"})
    @ResponseBody
    @CacheEvict(value= CommonCons.MyCache.MENUTREE, allEntries = true)
    public ApiResponse addResources() {
        try {
            resourceService.initAllResourceToCache();
            return ApiResponse.ofSuccess("缓存更新完成");
        } catch (Exception e) {
            throw new JsonException(Status.UNKNOWN_ERROR, "清楚初始化缓存错误");
        }
    }
}
